﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Model
{
    public class Wedding
    {
        static object locker = new object();
        public string identifier { get; set; }
        private List<WeixinUser> attends { get; set; }
        private List<WeixinUser> shares { get; set; }
        private List<Brand> brands { get; set; }

        public static Wedding New(string id)
        {
            Wedding w = new Wedding()
            {
                identifier = id,
                attends = new List<WeixinUser>(),
                shares = new List<WeixinUser>(),
                brands = new List<Brand>()
            };
            return w;
        }
        public int GetShareLayerFromUserId(string openid)
        {
            int index = shares.FindIndex(s =>
            {
                return s.access_token.openid == openid;
            });
            if(index != -1)
            {
                return shares[index].mylayer;
            }
            else
            {
                return -1;
            }
        }
        public bool AddUser(WeixinUser user)
        {
            bool success;
            lock(locker)
            {
                int index = attends.FindIndex(a =>
                    {
                        return a.access_token.openid == user.access_token.openid;
                    });
                if (index == -1)
                {
                    attends.Add(user);
                    success = true;
                }
                else
                    success = false;
            }
            return success;
        }
        public bool AddShare(WeixinUser share)
        {
            bool success;
            lock(locker)
            {
                int index = shares.FindIndex(s =>
                    {
                        return s.access_token.openid == share.access_token.openid;
                    });
                if (index == -1)
                {
                    shares.Add(share);
                    success = true;
                }
                else
                    success = false;
            }
            return success;
        }
        public uint UserCount
        {
            get
            {
                return (uint)attends.Count;
            }
        }
        public uint ShareCount
        {
            get
            {
                return (uint)shares.Count;
            }
        }
    }
}